<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>歌曲查询</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #song-list {
            margin-top: 20px;
        }
        .song-item {
            margin-bottom: 10px;
            cursor: pointer;
        }
        .song-item:hover {
            text-decoration: underline;
        }
        audio {
            margin-top: 20px;
            display: block;
        }
    </style>
</head>
<body>
    <h1>波d音乐</h1>
    <label for="song-name">输入歌曲名称：</label>
    <input type="text" id="song-name">
    <button onclick="searchSongs()">搜索</button>
    <div id="song-list">
        <!-- 歌曲列表将显示在这里 -->
    </div>
    <audio id="audio-player" controls></audio>
    <script>
        async function searchSongs() {
            const songName = document.getElementById('song-name').value.trim();
            if (!songName) {
                alert('请输入歌曲名称');
                return;
            }

            try {
                const targetUrl = `https://www.hhlqilongzhu.cn/api/dg_BDbdmusic.php?gm=${encodeURIComponent(songName)}&n=`;
                const response = await fetch(`https://api.allorigins.win/get?url=${encodeURIComponent(targetUrl)}`);
                if (!response.ok) {
                    throw new Error('网络响应错误');
                }

                const text = await response.json();
                const songs = parseSongs(text.contents);
                displaySongs(songs);
            } catch (error) {
                console.error('请求失败', error);
                alert('请求失败，请稍后重试');
            }
        }

        function parseSongs(text) {
            const lines = text.split('\n').filter(line => line.trim() !== '');
            const songs = lines.map(line => {
                const parts = line.split('--');
                const title = parts[0].replace(/^\d+、/, '').trim(); // 去掉前面的序号和逗号
                const artist = parts[1].trim();
                const index = line.match(/^\d+/)[0]; // 获取序号
                return { index, title, artist };
            });
            return songs;
        }

        function displaySongs(songs) {
            const songList = document.getElementById('song-list');
            songList.innerHTML = ''; // 清空之前的内容

            if (songs.length > 0) {
                songs.forEach(song => {
                    const songItem = document.createElement('div');
                    songItem.classList.add('song-item');
                    songItem.textContent = `${song.index}、${song.title} -- ${song.artist}`;
                    songItem.onclick = () => playSong(song.title, song.index);
                    songList.appendChild(songItem);
                });
            } else {
                songList.textContent = '未找到匹配的歌曲';
            }
        }

        async function playSong(title, index) {
            try {
                const targetUrl = `https://www.hhlqilongzhu.cn/api/dg_BDbdmusic.php?gm=${encodeURIComponent(title)}&n=${index}`;
                const response = await fetch(`https://api.allorigins.win/get?url=${encodeURIComponent(targetUrl)}`);
                if (!response.ok) {
                    throw new Error('网络响应错误');
                }

                const data = await response.json();
                const details = parseSongDetails(data.contents);
                alert(`歌名：${title}\n歌手：${details.artist}\n歌曲详情页：${details.detailUrl}\n播放链接：${details.playUrl}`);
            } catch (error) {
                console.error('请求失败', error);
                alert('请求失败，请稍后重试');
            }
        }

        function parseSongDetails(text) {
            const playUrlMatch = text.match(/播放链接：(.+)/);
            const playUrl = playUrlMatch ? playUrlMatch[1] : '';
            const titleMatch = text.match(/歌名：(.+)/);
            const artistMatch = text.match(/歌手：(.+)/);
            const detailUrlMatch = text.match(/歌曲详情页：(.+)/);
            const title = titleMatch ? titleMatch[1] : '';
            const artist = artistMatch ? artistMatch[1] : '';
            const detailUrl = detailUrlMatch ? detailUrlMatch[1] : '';
            return { title, artist, detailUrl, playUrl };
        }
    </script>
</body>
</html>
